package com.mjh.zhujianju.service.party.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.mjh.zhujianju.dao.party.BuildingExamResultMapper;
import com.mjh.zhujianju.model.party.BuildingExamEO;
import com.mjh.zhujianju.model.party.BuildingExamResultEO;
import com.mjh.zhujianju.model.party.BuildingExamTopicEO;
import com.mjh.zhujianju.model.party.BuildingExamUserEO;
import com.mjh.zhujianju.service.party.BuildingExamResultService;
import com.mjh.zhujianju.service.party.BuildingExamService;
import com.mjh.zhujianju.service.party.BuildingExamTopicService;
import com.mjh.zhujianju.service.party.BuildingExamUserService;
import com.mjh.zhujianju.vo.party.BuildingExamResultVO;
import com.mjh.zhujianju.vo.party.BuildingExamTopicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liuyang
 * @Description:
 * @date 2018/8/19 9:39
 */
@Service
public class BuildingExamResultServiceImpl extends BaseServiceImpl<BuildingExamResultEO> implements BuildingExamResultService {
    @Autowired
    private BuildingExamResultMapper buildingExamResultMapper;

    @Autowired
    private BuildingExamUserService buildingExamUserService;

    @Autowired
    private BuildingExamService buildingExamService;

    @Resource
    private BuildingExamTopicService buildingExamTopicService;

    public BuildingExamResultServiceImpl(BuildingExamResultMapper buildingExamResultMapper) {
        super(buildingExamResultMapper);
    }

    @Override
    public List<BuildingExamResultVO> selectTotalUsers() {
        return buildingExamResultMapper.selectTotalUsers();
    }

    @Override
    public BuildingExamResultVO queryBuildingExamResultList(Long examId, Long userId) {
        BuildingExamResultEO eo = new BuildingExamResultEO();
        eo.setExamId(examId);
        eo.setUserId(userId);
        List<BuildingExamResultEO> eoList = buildingExamResultMapper.selectListByCondition(eo);
        if (CollectionUtils.isEmpty(eoList)){
            return null;
        }
        BuildingExamResultVO vo = new BuildingExamResultVO();
        BeanUtils.copyProperties(eoList.get(0),vo);
        List<BuildingExamUserEO> buildingExamUserEOList = buildingExamUserService.queryBuildingExamUserByExamId(eoList.get(0).getId());
        vo.setList(buildingExamUserEOList);
        return vo;
    }

    @Override
    public List<BuildingExamResultEO> queryBuildingExamResultByExamId(Long examId) {
        return buildingExamResultMapper.selectExamResultsByExamId(examId);
    }

    @Override
    public List<BuildingExamResultEO> queryBuildingExamResultByUserIdList(Long userId) {
        return buildingExamResultMapper.selectExamResultsByUserId(userId);
    }

    @Override
    public Map<String, Object> queryBuildingExamResultDeatil(Long userId, Long examId) throws XTBusinessException {
        Map<String,Object> res = Maps.newHashMap();

        BuildingExamResultEO eo = new BuildingExamResultEO();
        eo.setExamId(examId);
        eo.setUserId(userId);
        List<BuildingExamResultEO> eoList = buildingExamResultMapper.selectListByCondition(eo);
        if (CollectionUtils.isEmpty(eoList)){
            return res;
        }
        eo =  eoList.get(0);

        BuildingExamEO buildingExamEO = buildingExamService.getByPrimaryKey(examId);

        res.put("resultId",eo.getId());
        res.put("examId",examId);

        //考试得分
        res.put("grade",eo.getTestScore());

        //及格分
        res.put("passingGrade",buildingExamEO.getPassingGrade());

        //是否通过：0-通过，1-不通过
        res.put("isPass",eo.getStatus());

        //考试用时
        res.put("testTime",eo.getTestTime());

         //查询对的题目
        List<BuildingExamTopicVO> trueTopicList = buildingExamTopicService.selectTopicAnswers(examId);

        List<BuildingExamUserEO> buildingExamUserEOList = buildingExamUserService.queryBuildingExamUserByExamId(eo.getId());

        Map<Long,String> resMap = Maps.newHashMap();
        Long count = 1L;
        for (BuildingExamTopicVO buildingExamTopic : trueTopicList){
            String ischeck = "false";

            if (buildingExamTopic.getType().equals("2")){
                //单选+判断
                boolean isAnswer=false;
                for (BuildingExamUserEO userCheck : buildingExamUserEOList){
                    if (buildingExamTopic.getAnswerId().equals(userCheck.getAnswerId())){
                        isAnswer=true;
                        continue;
                    }
                }
                if (isAnswer){
                    ischeck = "true";
                }
            }else {
                //多选
                boolean isAnswer=true;
                for (BuildingExamUserEO userCheck : buildingExamUserEOList){
                    if (!buildingExamTopic.getAnswerId().equals(userCheck.getAnswerId())){
                        isAnswer=false;
                        continue;
                    }
                }
                if (isAnswer){
                    ischeck = "true";
                }
            }
            resMap.put(count,ischeck);
            count++;
        }
        //改卷结果
        res.put("resMap",resMap);

        return res;
    }
}
